from time import sleep

import pytest
import requests

from api import log
from api.api_register_login import ApiRegisterLogin

from tools import common_assert, read_json, clear_data


class TestRegisterLogin:
    def setup_class(self):
        log.info("正在初始化数据库注册数据")
        # 调用清除方法
        clear_data()
        log.info("数据库注册数据清理完成")
    # 初始化
    def setup_method(self):
        # 获取session对象
        self.session = requests.session()
        log.info("正在获取session对象：{}".format(self.session))
        # 获取ApiRegisterLogin对象
        self.api = ApiRegisterLogin(self.session)
        log.info("正在获取ApiRegisterLogin对象：{}".format(self.api))

    # 结束
    def teardown_method(self):
        # 关闭session
        self.session.close()
        log.info("关闭session对象：{}".format(self.session))


    # 图片验证码接口测试
    @pytest.mark.parametrize("random,expect_code",read_json("register_login.json","img_code"))
    def test01_img_code(self,random,expect_code):
        try:
            result = self.api.api_img_code(random)
            log.info("图片验证码接口测试结果为:{}".format(result.status_code))
            common_assert(result, status_code=expect_code)
            log.info("图片验证码接口断言通过")
            print(result.status_code)
        except Exception as e:
            # 捕获异常的目的就是记录错误日志
            log.error(e)
            # 抛出异常
            raise

    # 短信验证码接口测试
    @pytest.mark.parametrize("phone,imgVerifyCode,expect_code",read_json("register_login.json","phone_code"))
    def test02_phone_code(self,phone,imgVerifyCode,expect_code):
        try:
            # 请求图片验证码
            self.api.api_img_code(123123)
            # 调用手机短信接口
            result = self.api.api_phone_code(phone, imgVerifyCode)
            log.info("短信验证码接口测试结果为:{}".format(result.text))
            common_assert(result, expect_json=expect_code)
            log.info("短信验证码接口断言通过")
            print(result.json())
        except Exception as e:
            log.error(e)
            raise

    # 注册接口测试
    @pytest.mark.parametrize("phone,password,verifycode,phone_code,expect_json",read_json("register_login.json","register"))
    def test03_register(self,phone,password,verifycode,phone_code,expect_json):
        try:
            # 请求图片验证码
            self.api.api_img_code(123456)
            # 调用手机短信接口
            self.api.api_phone_code(phone, verifycode)
            # 调用注册接口
            result = self.api.api_register(phone, password, verifycode, phone_code)
            log.info("注册接口测试结果为:{}".format(result.text))
            common_assert(result,expect_json=expect_json)
            log.info("注册接口断言通过")
            print(result.json())
        except Exception as e:
            log.error(e)
            raise

    # 登录接口测试
    @pytest.mark.parametrize("keywords,password,expect_json",read_json("register_login.json","login"))
    def test04_login(self,keywords,password,expect_json):
        # 调用登录接口
        try:
            i = 1
            result = None
            if password == "error123":
                while i <= 3:
                    result = self.api.api_login(keywords, password)
                    log.info("登录接口测试结果为:{}".format(result.text))
                    i+=1
                assert "锁定" in result.text
                sleep(60)
                result = self.api.api_login(keywords, password="test123")
                log.info("登录接口测试结果为:{}".format(result.text))
                common_assert(result, expect_json=expect_json)
                log.info("登录接口断言通过")
            else:
                result = self.api.api_login(keywords, password)
                log.info("登录接口测试结果为:{}".format(result.text))
                common_assert(result,expect_json=expect_json)
                log.info("登录接口断言通过")
            print(result.json())
        except Exception as e:
            log.error(e)
            raise

    # 查询登录状态接口测试
    @pytest.mark.parametrize("status,expect_json",read_json("register_login.json","login_status"))
    def test05_login_status(self, status,expect_json):
        try:
            if status == "已登录":
                # 调用登录接口
                self.api.api_login(keywords=13611110000, password="test123")
            # 调用查询登录状态接口
            result = self.api.api_login_status()
            log.info("查询登录状态接口测试结果为:{}".format(result.text))
            common_assert(result,expect_json=expect_json)
            log.info("查询登录状态接口断言通过")
            print(result.json())
        except Exception as e:
            log.error(e)
            raise

